using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using AvansRS.Domain;
using AvansRS.Domain.DataInterface;

namespace AvansRS.Web.Controllers
{
    public class RoosterController : Controller
    {
        IRepository<Rooster> mRoosterRepository;
        IRepository<TimeBlock> mTimeBlockRepository;

        public RoosterController(IRepository<Rooster> roosterRepository, IRepository<TimeBlock> timeBlockRepository)
        {
            mRoosterRepository = roosterRepository;
            mTimeBlockRepository = timeBlockRepository;
        }

        //
        // GET: /Rooster/

        public ActionResult Index()
        {
            //return View();
            IList<Rooster> roosters = mRoosterRepository.GetAll();
            //Session["type"] = "student";
            return View(roosters);
        }

        //
        // GET: /Rooster/RoosterCreate

        private MultiSelectList GetTimeBlocks(string[] selectedValues)
        {
            IList<TimeBlock> timeBlocks = mTimeBlockRepository.GetAll();

            return new MultiSelectList(timeBlocks, "ID", "sTime", selectedValues);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult RoosterCreate()
        {
            ViewData["timeblocks"] = GetTimeBlocks(null);

            return View();
        }

        //
        // POST: /Rooster/RoosterCreate

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult RoosterCreate([Bind(Exclude = "ID")] Rooster rooster, string[] TimeBlocks)
        {
            rooster.RoosterNumber = mRoosterRepository.GetAll().Count + 1;
            if (!ModelState.IsValid)
            {
                ViewData["timeblocks"] = GetTimeBlocks(null);
                return View();
            }

            IList<TimeBlock> timeB = new List<TimeBlock>(); ;
            foreach (string sT in TimeBlocks)
            {
                TimeBlock t = mTimeBlockRepository.GetByID(Int32.Parse(sT));
                timeB.Add(t);
            }

            rooster.TimeBlock = timeB;
            mRoosterRepository.Add(rooster);

            TempData["Message"] = "Rooster toegevoegd.";
            return RedirectToAction("Index", "Rooster");
        }


        // HourLayout
        //
        // GET: /Rooster/

        public ActionResult TimeBlockIndex()
        {
            IList<TimeBlock> hourLayout = mTimeBlockRepository.GetAll();

            return View(hourLayout);
        }

        //
        // GET: /Rooster/timeBlockCreate

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult TimeBlockCreate()
        {
            return View();
        }

        //
        // POST: /Rooster/timeBlockCreate

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimeBlockCreate([Bind(Exclude = "ID")] TimeBlock hourLayout)
        {
            if (!ModelState.IsValid)
                return View();

            mTimeBlockRepository.Add(hourLayout);

            TempData["Message"] = "TimeBlock toegevoegd.";
            return RedirectToAction("TimeBlockIndex", "Rooster");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult TimeBlockMultiCreate()
        {
            return View();
        }

        //
        // POST: /Rooster/timeBlockCreate

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimeBlockMultiCreate([Bind(Exclude = "ID")] TimeBlock hourLayout, int Minutes)
        {
            if (!ModelState.IsValid)
                return View();

            DateTime tmp = new DateTime();
            tmp = hourLayout.StartTime;
            int i = 1;
            while (true)
            {
                TimeBlock t = new TimeBlock();

                if (tmp.AddMinutes(Minutes).CompareTo(hourLayout.EndTime) <= 0)
                {
                    t.StartTime = tmp;
                    t.EndTime = tmp.AddMinutes(Minutes);
                    t.Description = i + " " + hourLayout.Description;
                    mTimeBlockRepository.Add(t);
                    tmp = tmp.AddMinutes(Minutes);
                }
                else
                    break;
                i++;
            }

            TempData["Message"] = "TimeBlocks toegevoegd.";
            return RedirectToAction("TimeBlockIndex", "Rooster");
        }

        //
        // GET: /Event/timeBlockEdit/5

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult TimeBlockEdit(int id)
        {
            TimeBlock ev = mTimeBlockRepository.GetByID(id);

            return View(ev);
        }

        //
        // POST: /Event/timeBlockEdit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimeBlockEdit(TimeBlock timeBlock, int locationID)
        {
            if (!ModelState.IsValid)
                return View(mTimeBlockRepository.GetByID(timeBlock.ID));

            TimeBlock tiRep = mTimeBlockRepository.GetByID(timeBlock.ID);
            mTimeBlockRepository.Save(timeBlock);


            TempData["Message"] = "timeBlock aangepast.";
            return RedirectToAction("TimeBlockIndex", "Rooster");
        }

        //
        // delete: /rooster/timeBlockDelete

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult TimeBlockDelete(int id)
        {
            TimeBlock timeBlock = mTimeBlockRepository.GetByID(id);
            return View(timeBlock);
        }

        //
        // POST: /rooster/timeBlockDelete

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TimeBlockDelete(TimeBlock timeBlock)
        {
            mTimeBlockRepository.Delete(timeBlock);

            TempData["Message"] = "TimeBlock met start-eindtijd '" + timeBlock.StartTime.ToString("HH:mm") + "-" + timeBlock.EndTime.ToString("HH:mm") + "' verwijderd.";
            return RedirectToAction("TimeBlockIndex", "Rooster");
        }
    }
}
